package com.myk.game.heroscuffle.skills;

import com.myk.game.heroscuffle.game.Hero;
import com.myk.game.heroscuffle.game.Skill;
import com.myk.game.heroscuffle.utils.RdmPropUtil;

public class Skill_ZhiLiao extends Skill {

    public static final int HEALING_AMOUNT = 60; //治疗量
    private Hero intentPatient;

    public Skill_ZhiLiao() {
        super("治疗", "指定一位其他英雄使其恢复一些hp，移除身上所有标记，跳过本回合，并能消除目标对自己的仇视。", 0, 5);
    }

    /**
     * 设置意向病人
     */
    public void setIntentPatient(Hero intentPatient) {
        this.intentPatient = intentPatient;
    }

    @Override
    protected void onTrigger() {
        if (hostHero.state.isPlayer) {
            selectTargetHero(0, true);
        }
        else {
            if (intentPatient == null) {
                intentPatient = game.pool.getAnyoneAliveHero(hostHero);
            }
            skillEffect(intentPatient);
        }
    }

    //玩家选择技能施放目标
    private void selectTargetHero(final int index, final boolean isFirst) {
        game.executor.submit(() -> {
            Hero curSelectHero = game.pool.getAllAliveHeroes().get(index);
            if (curSelectHero == hostHero) {
                int newIndex = index + 1;
                if (newIndex == game.pool.getAliveCount()) {
                    newIndex = 0;
                }
                selectTargetHero(newIndex, false);
            }
            game.ui.setTip("请选择你要治疗的目标,当前待选:" + curSelectHero.getObviousName());
            game.ui.resetCtrl();
            game.ui.addCtrl("选中", () -> game.executor.submit(() -> {
                skillEffect(curSelectHero);
                game.executor.resume(); //通知阻塞的线程继续执行以后的代码
            }));
            game.ui.addCtrl("切换", () -> game.executor.submit(() -> {
                int newIndex = index + 1;
                if (newIndex == game.pool.getAliveCount()) {
                    newIndex = 0;
                }
                selectTargetHero(newIndex, false);
            }));
        });
        if (isFirst) { //防止因为多次切换而阻塞多个线程
            game.executor.pause(); //阻塞线程直到接收到通知
        }
    }

    //技能效果
    private void skillEffect(Hero target) {
        final String hostName = hostHero.getObviousName();
        //恢复hp
        game.ui.printMsg("使" + target.getObviousName());
        target.addHpWithPrint(HEALING_AMOUNT);
        game.ui.sleepShortly();
        //移除标记
        if (target.SIGNS.size() > 0) {
            game.ui.printMsg("移除身上所有标记。");
            target.clearAllSign();
            game.ui.sleepShortly();
        }
        //跳过回合
        if (!target.state.isActioned) {
            game.ui.printMsg("躺下好好休息,跳过本回合。");
            target.setActioned();
            game.ui.sleepShortly();
        }
        //清除仇恨记录
        if (target.state.getAliveFeud() == hostHero) {
            target.clearFeud();
            game.ui.printMsg(target.getObviousName() + "放弃了对" + hostName + "的恩怨。");
        }
        if (target.state.recentEnemy == hostHero) {
            target.clearEnemy();
            game.ui.printMsg(target.getObviousName() + "放弃了对" + hostName + "的仇恨。");
        }
        if (target.state.recentPrey == hostHero) {
            target.clearPrey();
            game.ui.printMsg(target.getObviousName() + "放弃了对" + hostName + "的追击。");
        }
        game.ui.sleepShortly();
    }

    @Override
    protected void onUpdate() {

    }

    @Override
    protected void onInit() {

    }
}
